import { Component, OnInit } from '@angular/core';
import { Response} from '@angular/http';
import { Location} from '@angular/common';
import { RankingsService} from './rankings.service';
import {GlobalService} from "../../global.service";
import {ProblemRelatedService} from "../problem-related.service";
import {User} from "../../user/model/user-model";

@Component({
  selector: 'app-rankings',
  templateUrl: './rankings.component.html',
  styleUrls: ['./rankings.component.css']
})
export class RankingsComponent implements OnInit {

  queryData:JudgeInfo= new JudgeInfo;
  data:DataInfo[] = [];
  myData:DataInfo;
  total:number;
  url:string = this.location.path();
  currentUser:User;
  problemSetName:any;

  constructor(
    public location:Location,
    private globalService:GlobalService,
    private rankingsService:RankingsService,
    private problemRelatedService:ProblemRelatedService
  ) { }

  ngOnInit() {
    let url = this.url;
    let start = this.url.lastIndexOf('problem-sets/') + 13;
    let end = this.url.lastIndexOf('/rankings');
    this.currentUser = this.globalService.getCurrentUser();
    this.queryData.properties.problemSetCode = url.slice(start,end);
    if(this.globalService.getCurrentUser()!=null){
      this.queryData.properties.userCode = this.globalService.getCurrentUser().code;
    }
    this.queryData.properties.pageNumber = 0;
    this.queryData.properties.pageSize = 50;
    this.rankingsService.findRankOfProblemSet(this.queryData).subscribe((res:Response)=>{

      console.log(res.json());

      if(res.json().rows&&res.json().rows.length>0){

        /*if(res.json().rows[0].creator==this.globalService.getCurrentUser().code){
          this.myData = new DataInfo;
          this.myData.creator = res.json().rows[0].creator;
          this.myData.score = res.json().rows[0].score;
          this.myData.rowNo = res.json().rows[0].rowNo;
          this.myData.acceptNum = res.json().rows[0].acceptNum;
        }*/

        this.total = res.json().total;
        for(let i=0;i<res.json().rows.length;i++){
          var item = res.json().rows[i];
          this.data.push({
            rowNo:item.rowNo,
            creator:item.creator,
            score:item.score,
            acceptNum:item.acceptNum,
          });
        }
      }
    });
    //查找题目集信息
    let problemSetInfo = {code:url.slice(start,end)};
    this.problemRelatedService.getProblemsBySetsCode(problemSetInfo)
      .subscribe((res:Response)=>{
        let response = res.json();
        if (response.success){
          console.log("题目集信息:",response.value);
          this.problemSetName = response.value.name;
        }
      });
  }
}

class JudgeInfo{
  properties:Properties = new Properties;
}
class Properties{
  problemSetCode:string;
  userCode:string;
  pageNumber:number;
  pageSize:number;
}
class DataInfo{
  rowNo:string;
  creator:string;
  score:string;
  acceptNum:string;
}
